Ext.define('vsg.framework.button.SWFUploadButton', {
    extend: 'Ext.button.Button',
    alias: [
		'widget.swfupload.button',
		'widget.swfupload-button'
	],
    requires: [
        'Ext.button.Button'
    ],
	setting : {
		// Backend Settings
		upload_url: "../../data/upload/file",
		assume_success_timeout: 10,

		post_params: { 
			//"JSESSIONID": ""
		}, 	// Here are some POST values to send. These can be changed dynamically
		file_post_name: "Filedata",	// This is the "name" of the file item that the server-side script will receive. Setting this doesn't work in the Linux Flash Player
		requeue_on_error: false,
		http_success : [201, 202],

		// File Upload Settings
		file_size_limit : "100 MB",
		file_types : "*.*",
		file_types_description : "All Files",
		file_upload_limit : "10",

		button_width : 60,
		button_height : 25,
		button_window_mode : SWFUpload.WINDOW_MODE.WINDOW,
		button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
		
		//button_image_url : "../../resources/images/upload_file.png",
		button_text : '<span class="btnText">添加附件</span>',
		button_text_style : ".btnText { font-size: 10;  font-family: tahoma, arial, verdana, sans-serif;}",
		button_text_bottom_padding : 3,
		button_text_left_padding : 10,
		

		
		// Flash Settings
		flash_url : "../../js/swfupload/swfupload.swf",	// Relative to this file

		// Debug Settings
		debug: true		// For the purposes of this demo I wan't debug info shown
	},
	uploadAllQueued : false,
	
	
	constructor: function (config) {
		var me = this;
		me.selectedFiles = new Ext.util.MixedCollection();
		
		if (config.base_setting) {
			for (var i in config.base_setting) {
				me.setting[i] = config.base_setting[i];
			}
			//Ext.applyIf(me.setting, config.base_setting);
		}
		me.setting['file_dialog_start_handler'] = me.fileDialogStart;
		me.setting['file_queued_handler'] = me.fileQueued;
		me.setting['file_queue_error_handler'] = me.fileQueueError;
		me.setting['file_dialog_complete_handler'] = me.fileDialogComplete;

		me.setting['upload_progress_handler'] = me.uploadProgress;
		me.setting['upload_error_handler'] = me.uploadError;
		me.setting['upload_success_handler'] = me.uploadSuccess;
		me.setting['upload_complete_handler'] = me.uploadComplete;
		
		if (me.swfUploadLoaded) {
			me.setting['swfUploadLoaded'] = me.swfUploadLoaded;
		}
	  
		me.callParent(arguments);
		
    },	

   // inherit docs
    initComponent: function() {
        var me = this;

		if (typeof(SWFUpload) === "undefined") {
			Ext.log({
				msg:'js/swfupload/swfupload.js not loading!'
			});
			return;
		}
		
       	me.callParent(arguments);
	
		// --- rebuild arguments ---
       Ext.applyIf(me, {
			listeners : {
				afterrender: {
					fn:me.afterrender,
					scope:me
				}
			}
	   });
	   
    },
	
	afterrender:function(self, eOpts) {
		var me = this;
		var id = self.getId()+'-btnInnerEl';
		me.setting['button_placeholder_id'] = id;
		me.setting['debug_handler'] = me.debugSWF;
		
		me.setting['custom_settings'] = {
			bindCurComp : me
		}
		
		// --- fix bug for java session ---
		/*
		var jesIdLoc = me.setting['upload_url'].indexOf(';JSESSIONID=');
		if (jesIdLoc == -1) {
			url = me.setting['upload_url'];
			url +=';JSESSIONID=' + me.setting['post_params']['JSESSIONID'];
			me.setting['upload_url'] = url;
		}
		*/
	
		me.suo = new SWFUpload(me.setting);

	},
	
	addSelectedFiles:function(file) {
		var me = this;
		me.selectedFiles.add(file['id'],file);
	},
	
	getSelectedFiles:function() {
		var me = this;
		return me.selectedFiles;
	},
	
	getSWFUpload:function() {
		var me = this;
		return me.suo;
	},
	
	
	setUploadAllQueued:function(newValue) {
		var me = this;
		me.uploadAllQueued = newValue;
	},
	
	getUploadAllQueued:function() {
		var me = this;
		return me.uploadAllQueued;
	},
	
	// --- swf upload handler define ---
	fileDialogStart:function() {
		
	},
	
	fileQueued:function(file) {
		try {
			var customSetting = this.customSettings;
			var swfUploadBtn = customSetting['bindCurComp'] ;
		
			swfUploadBtn.addSelectedFiles(file);	
		
			// --- fire event ---
			swfUploadBtn.fireEvent('fileQueued',swfUploadBtn, file );
			
		} catch (ex) {
			this.debug(ex);
		}
	},
	
	fileQueueError:function() {
		
	},
	
	fileDialogComplete:function(selectedFileNum, queuedFileNum, totalQueuedFileNum) {
		try {
			var customSetting = this.customSettings;
			var swfUploadBtn = customSetting['bindCurComp'] ;
		
			// --- fire event ---
			swfUploadBtn.fireEvent('fileDialogComplete',swfUploadBtn, {
				selectedFileNum : selectedFileNum,
				queuedFileNum : queuedFileNum,
				totalQueuedFileNum : totalQueuedFileNum
			} );
			
		} catch (ex) {
			this.debug(ex);
		}		
	},
	
	
	uploadProgress:function(file,completeBytes, totalByte) {
		try {
			var customSetting = this.customSettings;
			var swfUploadBtn = customSetting['bindCurComp'] ;
		
			// --- fire event ---
			swfUploadBtn.fireEvent('uploadProgress',swfUploadBtn, {
				file : file,
				completeBytes : completeBytes,
				totalByte : totalByte
			} );
			
		} catch (ex) {
			this.debug(ex);
		}			
	},
	
	uploadError:function(fileObject, errorCode, message) {
		try {
			var customSetting = this.customSettings;
			var swfUploadBtn = customSetting['bindCurComp'] ;
		
			// --- fire event ---
			swfUploadBtn.fireEvent('uploadError',swfUploadBtn, {
				file : fileObject,
				errorCode : errorCode,
				message :message
			} );
			
		} catch (ex) {
			this.debug(ex);
		}		
	},
	
	uploadSuccess:function(fileObject, serverData, receivedResponse) {
		try {
			var customSetting = this.customSettings;
			var swfUploadBtn = customSetting['bindCurComp'] ;
			
			// --- run and check the value again ---
			var uploadAllQueued = swfUploadBtn.getUploadAllQueued();
			if ( uploadAllQueued ) {
				var status = this.getStats();
				var queuedStatus = status['files_queued'];
				if (queuedStatus > 0) {
					this.startUpload();
				}
			}
		
			// --- fire event ---
			swfUploadBtn.fireEvent('uploadSuccess',swfUploadBtn, {
				file : fileObject,
				serverData : serverData,
				receivedResponse :receivedResponse
			} );
			
		} catch (ex) {
			this.debug(ex);
		}			
	},
	
	uploadComplete:function(fileObject) {
		try {
			var customSetting = this.customSettings;
			var swfUploadBtn = customSetting['bindCurComp'] ;
		
			// --- fire event ---
			swfUploadBtn.fireEvent('uploadComplete',swfUploadBtn, {
				file : fileObject
			} );
			
		} catch (ex) {
			this.debug(ex);
		}		
	},
	
	
	debugSWF:function(message) {
		console.debug(  message );
	}


});